﻿using S.Archives.Domain.S.ArchivesModel;
using S.EarTag.Domain.Dto;
using S.EarTag.Domain.Model;
using S.EarTag.Infrastructure.Repository.Interface.IEarTagRepository;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace S.EarTag.Infrastructure.Repository.Impl.EarTagRepository
{
    /// <summary>
    /// 育种称重
    /// </summary>
    public class BreedingWeighRepository : BaseRepository<BreedingWeigh>, IBreedingWeighRepository
    {
        private readonly SqlSugarClient db;

        public BreedingWeighRepository(SqlSugarClient db) : base(db)
        {
            this.db = db;
        }

        public async Task<bool> AddBreeding(BreedingWeigh breeding)
        {
            breeding.CreateBy = "蔡志坤";
            breeding.CreateDate= DateTime.Now;
            var result =  await db.Insertable(breeding).ExecuteReturnIdentityAsync();
            return result.ObjToBool();
        }
        
        public async Task<(IEnumerable<QueryResult> items, int totalCount, int pageCount)> BreedingList(string? startDate, string? endDate, string EarTag, int WeighCategory, int VarietyEnum, int Sex, int page, int size)
        {
            var result = db.Queryable<BreedingWeigh, SheepFileModel>((bw, sfm) => new JoinQueryInfos(JoinType.Left, bw.EarTag == sfm.EarTag)).Select<QueryResult>((bw, sfm) => new QueryResult
            {
                BreedingID = bw.BreedingID,
                BirthDate = sfm.BirthDate,
                SheepFileId = sfm.SheepFileId,
                WeighDate = bw.WeighDate,
                EarTag = bw.EarTag,
                Variety = sfm.Variety,
                Sex = sfm.Sex,
                WeighCategory = bw.WeighCategory,
                Weight = bw.Weight,
                WeightMonthsAge = bw.WeightMonthsAge,
                CreateBy = bw.CreateBy,
                CreateDate = bw.CreateDate,
                UpdateBy = bw.UpdateBy,
                UpdateDate = bw.UpdateDate,
                LogicalDeletion = bw.LogicalDeletion 
            }).ToList();
            if (!string.IsNullOrEmpty(startDate))
            {
                result = result.Where(a => a.WeighDate > DateTime.Parse(startDate)).ToList();
            }
            if (!string.IsNullOrEmpty(endDate))
            {
                result = result.Where(a => a.WeighDate < DateTime.Parse(endDate)).ToList();
            }
            if (!string.IsNullOrEmpty(EarTag))
            {
                result = result.Where(a => a.EarTag.Contains(EarTag)).ToList();
            }
            if (WeighCategory!=0)
            {
                result = result.Where(a => ((int)a.WeighCategory) == WeighCategory).ToList();
            }
            if (VarietyEnum != 0)
            {
                 result = result.Where(a => a.Variety == VarietyEnum).ToList();
            }
            if (Sex != 0)
            {
                result = result.Where(a => a.Sex == Sex).ToList();
            }
            var totalCount = result.Count;
            var pageCount = (int)Math.Ceiling(totalCount * 1.0 / size);
            result = result.Skip((page - 1) * size).Take(size).ToList();
            return (result, totalCount, pageCount);
        }

        public async Task<bool> DeleteBreeding(int id)
        {
            var result = await db.Deleteable<BreedingWeigh>().In(id).ExecuteCommandAsync();
            return result.ObjToBool();
        }

        public async Task<bool> UpdateBreeding(BreedingWeigh breeding)
        {
            breeding.UpdateBy = "蔡志坤";
            breeding.UpdateDate = DateTime.Now;
            var result = await db.Updateable(breeding).ExecuteCommandAsync();
            return result.ObjToBool();
        }


        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> BatchAddBreedingWeigh(List<BreedingWeigh> model)
        {
            var result = await db.Insertable(model).ExecuteCommandAsync();
            return result > 0;
        }


    }
}
